package com.rowenci.iot_agriculture.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.rowenci.iot_agriculture.bean.admin.Admin;
import com.rowenci.iot_agriculture.service.api.AdminService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping(value = "/admin")
public class AdminController {

    @Resource
    private AdminService adminService;

    @GetMapping(value = "/login")
    public String login() throws Exception {
        return "login";
    }

    /**
     * 登录
     *
     * @param account
     * @param password
     * @param model
     * @param request
     * @return
     * @throws Exception
     */
    @PostMapping(value = "/login_check")
    @ResponseBody
    public String login_check(String account, String password, Model model, HttpServletRequest request) throws Exception {
        /*String account = request.getParameter("account");
        String password = request.getParameter("password");*/
        Admin admin = adminService.findAdmin(account, password);
        HttpSession session = request.getSession();
        if (admin == null) {
            model.addAttribute("msg", "fault");
            return JSON.toJSONString(model);
        } else {
            model.addAttribute("msg", "success");
            session.setAttribute("usersession", account);
            System.out.println(session.getAttribute("usersession"));
            return JSON.toJSONString(model);
        }
    }

    @GetMapping(value = "/login_success")
    public String login_success() throws Exception {
        return "index";
    }

    /**
     * 注册
     *
     * @param admin
     * @param model
     * @return
     * @throws Exception
     */
    @PostMapping(value = "/regist")
    @ResponseBody
    public String regist(Admin admin, Model model) throws Exception {

        if (adminService.addAdmin(admin) == false) {
            model.addAttribute("msg", "fault");
            /**
             * 这里如果将第二个参数去掉的话会报错
             * 具体原因不清楚，查询后得知如下：
             * 当反射方法返回类型为List<Stock>时，fastjson解析Stock时失败
             * fastjson解析Stock时调用get方法时会出现异常。
             *
             * 第一种方法就是加上第二个参数
             * 第二种方法是自己进行解析List<Stock>
             *
             * 难受的是我也不清楚为什么其他方法不报错，就这个注册报错 - -
             */
            return JSON.toJSONString(model, SerializerFeature.IgnoreNonFieldGetter);
        } else {
            model.addAttribute("msg", "success");
            return JSON.toJSONString(model, SerializerFeature.IgnoreNonFieldGetter);
        }

    }

    /**
     * 登出
     *
     * @param model
     * @param request
     * @return
     * @throws Exception
     */
    @GetMapping(value = "/logout")
    @ResponseBody
    public String logout(Model model, HttpServletRequest request) throws Exception {

        HttpSession session = request.getSession();
        if (session.getAttribute("usersession") == null) {
            model.addAttribute("msg", "fault");
            return JSON.toJSONString(model);
        } else {
            model.addAttribute("msg", "success");
            session.removeAttribute("usersession");
            return JSON.toJSONString(model);
        }

    }

    /**
     * 修改密码
     *
     * @param account
     * @param password
     * @param securityno
     * @param model
     * @return
     * @throws Exception
     */
    @PostMapping(value = "/changepwd")
    @ResponseBody
    public String changePWD(String account, String password, String securityno, Model model) throws Exception {
        if (adminService.changePassword(account, securityno, password) == false) {
            model.addAttribute("msg", "fault");
            return JSON.toJSONString(model);
        } else {
            model.addAttribute("msg", "success");
            return JSON.toJSONString(model);
        }
    }

    /**
     * 修改安全码
     *
     * @param account
     * @param securityno_old
     * @param securityno_new
     * @param model
     * @return
     * @throws Exception
     */
    @PostMapping(value = "/changesecno")
    @ResponseBody
    public String changeSecNo(String account, String securityno_old, String securityno_new, Model model) throws Exception {
        if (adminService.changeSecurityNo(account, securityno_old, securityno_new) == false) {
            model.addAttribute("msg", "fault");
            return JSON.toJSONString(model);
        } else {
            model.addAttribute("msg", "success");
            return JSON.toJSONString(model);
        }
    }
}
